﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class MetadataExpression : MetadataSingleArgumentSuffix<StringLiteral>
{
    private SyntaxToken? _featureKeywordToken;

    internal MetadataExpression(SyntaxToken declareKeywordToken, SyntaxNode? parent) :
        base(SyntaxKinds.MetadataExpression,
            declareKeywordToken,
            NodeConverter.ToIdentifier(declareKeywordToken),
            parent)
    {
    }


    /// <summary>
    ///     deriveelements 或者 noderiveelements 关键字
    /// </summary>
    public SyntaxToken? FeatureKeywordToken => _featureKeywordToken;


    public MetadataExpression Update(SyntaxToken featureKeywordToken)
    {
        Debug.Assert(featureKeywordToken.IsOneOfNode(SyntaxKinds.DeriveElementsKeywordToken,
            SyntaxKinds.NoDeriveElementsKeywordToken));
        SetChildNullableField(ref _featureKeywordToken, featureKeywordToken);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        base.AcceptVisitor(visitor);
        Accept(visitor, FeatureKeywordToken);
    }
}


public static partial class NodeFactory
{
    public static MetadataExpression CreateMetadataExpression(SyntaxNode? parent, SyntaxToken declareKeywordToken,
        StringLiteral? value, SyntaxToken? featureKeywordToken)
    {
        Debug.Assert(declareKeywordToken.IsNode(SyntaxKinds.ExpressionKeywordToken));
        MetadataExpression expr = new(declareKeywordToken, parent);
        if (featureKeywordToken != null)
        {
            expr.Update(featureKeywordToken);
        }

        return UpdateMetadataSingleArgumentSuffix(expr, value);
    }
}